import { requestClient } from '#/api/request';

export namespace CommonModelApi {
  export interface QuerySelectTableParams {
    // 表名
    tableName: string;
    // label字段名
    labelField: string;
    // value字段名
    valueField: string;
    // 条件查询
    sqlSelect: string;
  }

  export interface QuerySelectTreeTableParams extends QuerySelectTableParams {
    // 树级结构，父级ID的字段名
    parentField: 'pid' | string;
    // 排序字段,可不传
    sortRankField: string;
    sortRankWay: 'asc' | string;
  }

  export interface ValidExistDataParams {
    tableName: string;
    fieldName: string;
    fieldVal: string;
    rowId: string;
    sqlSelect: string;
  }

  export interface ParamsType {
    fieldNames: string;
  }

  export interface ParamsGroupType {
    key: string;
  }
}

/**
 * 查询表中数据
 * @param params 参数
 */
export async function querySelectTableApi(
  params: CommonModelApi.QuerySelectTableParams,
) {
  return requestClient.get('/sys/querySelectTable', { params });
}

/**
 * 查询树表中数据
 * @param params 参数
 */
export async function querySelectTreeTableApi(
  params: CommonModelApi.QuerySelectTreeTableParams,
) {
  return requestClient.get('/sys/querySelectTreeTable', { params });
}

/**
 * 验证是否存在数据
 * @param params 参数
 */
export async function validExistDataApi(
  params: CommonModelApi.ValidExistDataParams,
) {
  return requestClient.get('/sys/validExistData', { params });
}

/**
 * 根据key获取一组配置参数
 * @param params key
 */
export async function queryGroupParamsByKeyApi(
  params: CommonModelApi.ParamsGroupType,
) {
  return requestClient.get(`/sys/getGroupParamsByKey`, { params });
}

/**
 * 根据配置的字段获取输入的字
 * @param params 参数
 */
export async function queryParamsByFieldNameApi(
  params: CommonModelApi.ParamsType,
) {
  return requestClient.get(`/sys/getParamsByFieldName`, { params });
}

/**
 * 上传文件
 * @param params 参数
 */
export async function uploadFileApi(params: any) {
  return requestClient.post(`/sys/upload`, params, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}
